	var map;
    var isShowed = false;
    var isFirst = true;
    centerX = 21.0333333;
    centerY = 105.85000000000002;
    function initialize(centerX,centerY) {
    	//alert(centerX);
        var myOptions = {
          center: new google.maps.LatLng(centerX,centerY),
          zoom: 16,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_detail"),
            myOptions);
      }
	function getContentMiniInfoWindow(id, name, image, description,numberView) {
	    var content = "<div class='InfoWindow'><div class='InfoWindowImage'><img alt='/' class='BigImage' src='" + image + "' /></div><div class='DivContent'><div class='DivTitle'><b>" + name + "</b></div><div class='DivDescription'><p>" + description + "</p></div><div class='DivSubmit'></div></div></div>";
	    return content;
	}
    function getLocation()
      {
        var add = $("#add").val();
        if (add == '') return;
        var geocoder = new google.maps.Geocoder();
    	geocoder.geocode({ 'address': add }, function (results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
		    var location = results[0].geometry.location;
			var marker = new google.maps.Marker({
				position: location,
				title: name
	    		});
	    	marker.setMap(map);
            var centerX =  location.lat();
            var centerY = location.lng();
            var latLng = new google.maps.LatLng(centerX, centerY);
            map.setCenter(latLng);
			google.maps.event.addListener(marker, 'click', function () {
				var id = '1';
				var name = $("#gname").val();
				var description = $("#gdescription").val();
				var numberView = 10;
				var image = '/Upload/Offer/'+$("#gimage").val();
				var contentString  = getContentMiniInfoWindow(id, name, image, description, numberView);
        			var infoWindow = new google.maps.InfoWindow({ content: contentString });
				infoWindow.open(map, marker);
				
    			});
		} else {
		    alert("Geocode was not successful for the following reason: " + status);
		}
    	});
      }
    
    // Show vung div chua ban do

    function showMapArea() {
        if (!isShowed) {
            $("#map_detail").show("slow", function () {
            	if(isFirst)
            	{
            		initialize(centerX,centerY);
            		isFirst = false;
            	}
                //startedMap = true;
                isShowed = true;
            });
        }
    }
    function getAdd()
    {
    	var add = $("#add").val();
    	if (add == '') return;
        var geocoder = new google.maps.Geocoder();
     	geocoder.geocode({ 'address': add }, function (results, status) 
     	{
     		if (status == google.maps.GeocoderStatus.OK) 
     		{
     			var location = results[0].geometry.location;
     			//var marker = new google.maps.Marker({
 				//position: location,
 				//title: name
 	    		//});
     			//marker.setMap(map);
     			centerX =  location.lat();
     			centerY = location.lng();
     			initialize(centerX,centerY);
     			//alert(centerX);
     		}
 		});
     	//alert(tempX);
    }
    /*********************************************************************************
    * Show map area with location
    *********************************************************************************/
    function showMapAreaWithLocation()//(location, id, name, image, description) 
    {
    	if (!isShowed) {
            $("#map_detail").show("slow", function () {
            	if(isFirst)
            	{
            		getAdd();
            		//alert(centerX);
            		//initialize();
            		isFirst = false;
            	}
                //startedMap = true;
            	
            	getLocation();
                isShowed = true;
            });
        }
        else 
        {
            //detectLocation(location, id, name, image, description);
        	getLocation();
        }
        topRelativePos = 500;
    }
    function closeMapArea() {
        if (isShowed == true) {
            $('#map_detail').hide("slow", function () {
                topRelativePos = 10;
            });
            isShowed = false;
        }
    }
    
    
    $(document).ready(function(){  

    $(function(){
    	 $("#btShowMap").click(function () {
 	        if (!isShowed) 
 	        {
 	            showMapArea();
 	            $('#btShowMap').text("Close Map");
 	            $('#imgShowCloseMap').attr("src", "../Content/Image/migiv2/hidden-map.png");
 	        }
 	        else 
 	        {
 	            $('#btShowMap').text("Show Map");
 	            closeMapArea();
 	            $('#imgShowCloseMap').attr("src", "../Content/Image/migiv2/show-map.png");
 	        }
 	    });
    	$("#btFindMap").click(function(){
    			showMapAreaWithLocation();
	            $('#btShowMap').text("Close Map");
	            $('#imgShowCloseMap').attr("src", "../Content/Image/migiv2/hidden-map.png");
        });
	   
    });
    });


